using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics.Contracts;
using Konst3d.Efficiency;
using Konst3d.Efficiency.Suite;

// http://www.csharp-examples.net/string-format-double/

namespace PEM
{
	/// <summary>
	/// Interaction logic for StatisticDialog.xaml
	/// </summary>
	public partial class StatisticDialog : PersistentModalDialog
	{
		public StatisticDialog()
		{
			InitializeComponent();
		}

		public bool ShowDialog(Window owner, IMonitor monitor)
		{
			if (monitor != null)
			{
				_model = new MonitoringStatisticViewModel(monitor, this);
				DataContext = _model;
			}
			else
			{
				DataContext = null;
			}

			return ShowDialog(owner);
		}

		private void OnCreateButtonClick(object sender, RoutedEventArgs e)
		{
			DialogResult = true;
		}

		private void OnCancelButtonClick(object sender, RoutedEventArgs e)
		{
			DialogResult = false;
		}

		private void OnStatisticItemMouseDoubleClick(object sender, MouseButtonEventArgs e)
		{
			if (_model.Commands.EditSelectedDay.CanExecute(null))
			{
				_model.Commands.EditSelectedDay.Execute(null);
			}
		}

		#region Fields

		private MonitoringStatisticViewModel _model;

		#endregion // Fields
	}

	public class DateToBrushConverter : IValueConverter
	{
		#region IValueConverter Members

		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value != null)
			{
				var date = (DateTime)value;

				if (date <= DateTime.Now)
				{
					return (date.DayOfWeek == DayOfWeek.Sunday || date.DayOfWeek == DayOfWeek.Saturday) ? new SolidColorBrush(Colors.LightCoral) : new SolidColorBrush(Colors.White);
				}
				else
				{
					return new SolidColorBrush(Colors.LightGray);
				}
			}

			return new SolidColorBrush(Colors.White);
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}

		#endregion
	}

	public class StatisticEntryColorCalculator : IMultiValueConverter
	{
		#region IMultiValueConverter Members

		public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			// assuming values[0] - monitor created date
			//          values[1] - entry date

			if (values != null)
			{
				var created = ((DateTime)values[0]).GetShortDate();
				var date = ((DateTime)values[1]).GetShortDate();
				var now = DateTime.Now.GetShortDate();
				var dayOfWeek = ((DateTime)values[1]).DayOfWeek;

				if (date >= created && date <= now)
				{
					return (dayOfWeek == DayOfWeek.Sunday || dayOfWeek == DayOfWeek.Saturday) ? new SolidColorBrush(Colors.LightCoral) : new SolidColorBrush(Colors.White);
				}
				else
				{
					return new SolidColorBrush(Colors.LightGray);
				}
			}

			return new SolidColorBrush(Colors.White);
		}

		public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}

		#endregion
	}

}
